<?php
namespace Heatingfee\Controller;
use Think\Controller;
class IndexController extends Controller {
	//企业号corpid
	public $corpid = null;
	//企业号corpsecret
	public $corpsecret = null;
                
	public function __construct(){
            parent::__construct();
            $this -> corpid = C('CORPID');
            $this -> corpsecret  = C('SECRET');
            //echo createAuthUrl($this -> corpid,'Heatingfee','Index','index');die;
	}
	//获取Access_token
	public function Access_token($corpid,$corpsecret){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret";
            $access_token = json_decode(file_get_contents($url),true)['access_token'];
            return $access_token;
	}

	//获取用户userid
	public function Getuserid(){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".session('Heatingfee_access_token')."&code=".session('Heatingfee_code');
            $userid = json_decode(file_get_contents($url),true)['UserId'];
            return $userid;
	}

	//获取用户详细信息
	public function Getinfo($access_token,$userid){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".session('Heatingfee_access_token')."&userid=$userid";
            $userinfo = json_decode(file_get_contents($url),true);
            return $userinfo;
	}

	//通过部门ID 获取部门名称
	public function Getbumen($id){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".session('Heatingfee_access_token')."&id=$id";
            $bumen = json_decode(file_get_contents($url),true);
            return $bumen;
	}

	//获取Code 通过Code 获取用户名  当作条件去数据库里取相应数据
	public function index(){
        if(!$userid  = session('Heatingfee_userid')){
            $code = $_GET['code'];
            session('Heatingfee_code',$code);
            $access_token = $this -> Access_token($this -> corpid, $this -> corpsecret );
            session('Heatingfee_access_token',$access_token);
            $userid = $this -> Getuserid();
            session('Heatingfee_userid',$userid);
        }
        /*测试数据*/
        $userid = 'HeYing';

        $Model = D('qnf');
        $res = $Model -> where("userid = '$userid' ") -> select();
        $res = end($res);
        $this -> assign('data',$res);
        $this -> display();
    }

    //按年份查询相应数据  查询出结果可能是多个 默认取最后一个
    public function search(){
        $keywords = $_GET['keywords'];
        $Model = M('qnf');
        $userid = session('Heatingfee_userid');

        /*测试数据*/
        $userid = 'HeYing';

        $res = $Model -> where("nf = '$keywords' AND userid = '$userid' ") -> select();
        $this -> assign('datas',end($res)); 
        $this -> assign('keywords',$keywords);
        $this -> display();
    }
    
    
    public function test(){
         $data = [
                    0 => ['字段1','字段2','字段3'], 
                    1 => ['值1','值2','值3'],        
                    2 => ['值1','值2','值3'],
            ];
         
     A('Tool/Excel') -> writeExcel( $data);
    }
}

